package logic

import (
	"context"
	"fmt"
	"nove/models/mysql"

	"nove/noverpc/internal/svc"
	"nove/noverpc/noverpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookAddLogic {
	return &BookAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookAddLogic) BookAdd(in *noverpc.BookAddRequest) (*noverpc.BookAddResponse, error) {
	// todo: add your logic here and delete this line

	if in.Title == "" || in.Kind == "" || in.Intro == "" {
		return nil, fmt.Errorf("必填字段（标题、分类、简介）")
	}
	
	book := mysql.Book{
		Title: in.Title,
		Kind:  in.Kind,
		Name:  in.Name,
		Intro: in.Intro,
	}
	mysql.DB.Debug().Create(&book)

	return &noverpc.BookAddResponse{
		BookId: int64(book.ID),
	}, nil
}
